package com.topscore.uke.ui.activities;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.entities.SalesRankingItem;
import com.topscore.uke.mvp.contractors.GetMyPerformanceContractor;
import com.topscore.uke.mvp.presenters.GetMyPerformanceDataPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.widgets.RecyclerAdapterLoadMoreView;
import com.topscore.uke.utils.DisplayImageOptionsManager;
import com.topscore.uke.utils.SnackBarUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.view.ComboLineColumnChartView;

public class MySalePerformanceActivity extends ToolBarCompatActivity implements GetMyPerformanceContractor.View {

    @Bind(R.id.performance_chart)
    ComboLineColumnChartView mPerformanceChart;
    //    @Bind(R.id.toolbar_layout)
//    CollapsingToolbarLayout toolbarLayout;
    @Bind(R.id.app_bar)
    AppBarLayout appBar;
    @Bind(R.id.ranking_recycler)
    RecyclerView rankingRecycler;
    @Bind(R.id.total_sales_amount)
    TextView totalSalesAmount;
    @Bind(R.id.total_sales_count)
    TextView totalSalesCount;
    @Bind(R.id.total_return_amount)
    TextView totalReturnAmount;
    @Bind(R.id.total_return_count)
    TextView totalReturnCount;

    private BaseQuickAdapter<SalesRankingItem, BaseViewHolder> mRankingAdapter;

    private GetMyPerformanceContractor.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sale_performance);
        ButterKnife.bind(this);
        mPresenter = new GetMyPerformanceDataPresenter(this);
        setCentralTitle("销售订单");
        initViews();
    }

    private void initViews() {
        rankingRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rankingRecycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        initAdapter();
        rankingRecycler.setAdapter(mRankingAdapter);
        initData();
    }

    private void initAdapter() {
        mRankingAdapter = new BaseQuickAdapter<SalesRankingItem, BaseViewHolder>(R.layout.sales_ranking_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder holder, SalesRankingItem item) {
                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                holder.setText(R.id.title_tv, item.getProductName())
                        .setText(R.id.price_tv, "￥" + decimalFormat.format(item.getMoney()))
                        .setText(R.id.sales_count, String.format("已销售%d双", item.getProductCount()))
                        .setText(R.id.visited_count, String.format("浏览人次为%d", item.getAccessCount()));
                ImageLoader.getInstance().displayImage(item.getProductImg(), (ImageView) holder.getView(R.id.image_iv), DisplayImageOptionsManager.getInstance().getDefaultOptions());

            }
        };

        mRankingAdapter.setLoadMoreView(new RecyclerAdapterLoadMoreView());
        mRankingAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) rankingRecycler.getParent(), false));
        mRankingAdapter.setEnableLoadMore(true);
        mRankingAdapter.setOnLoadMoreListener(() -> {
        }, rankingRecycler);
    }

    private void initData() {
        mPresenter.getSalesData();
    }

    @Override
    public void onLoading(String loadingMessage) {
        showLoadingDialog(loadingMessage);
    }


    @Override
    public void onLoadFinished() {
        dismissLoadingDialog();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        SnackBarUtil.getInstance(rankingRecycler)
                .showLong(errorMessage)
                .setAction("重试", v -> initData())
                .show();

//        TopSnackbar.make(getToolbar(), errorMessage, TopSnackbar.LENGTH_LONG, TopSnackbar.APPEAR_FROM_TOP_TO_DOWN)
//                .setAction("重试", v -> initData())
//                .setMinHeight((int) getResources().getDimension(R.dimen.toolbar_padding_top), getToolbar().getHeight())
//                .show();
    }

    @Override
    public void onGetChartDataSuccess(ComboLineColumnChartData lineColumnChartData) {
        mPerformanceChart.setComboLineColumnChartData(lineColumnChartData);
    }

    @Override
    public void onGetTotalSalesCountSuccess(String salesCount) {
        totalSalesCount.setText(salesCount);
    }

    @Override
    public void onGetTotalSalesAmountSuccess(String salesAmount) {
        totalSalesAmount.setText(salesAmount);
    }

    @Override
    public void onGetTotalReturnCountSuccess(String returnCount) {
        totalReturnCount.setText(returnCount);
    }

    @Override
    public void onGetTotalReturnAmountSuccess(String returnAmount) {
        totalReturnAmount.setText(returnAmount);
    }

    @Override
    public BaseQuickAdapter getRankingAdapter() {
        return mRankingAdapter;
    }

    @Override
    public void onGetRankListSuccess(List<SalesRankingItem> rankingList) {
        mRankingAdapter.setNewData(rankingList);
    }
}
